<?php

namespace My\Model;

use Utuo\UtuoModel;

class DistributionShopModel extends UtuoModel {
    
    public $extendsConfig = array();
    
    // 构造函数
    public function __construct($name = '', $tablePrefix = '', $connection = '') {
        parent::__construct($name, $tablePrefix, $connection);
        $this->extendsConfig = array(
              'indexAdv' => array(
                    'type'=>'textEditor',
                    'title' => '首页弹出广告',
                    'name'=>'indexAdv',
                    'value'=>'',
              ),
        );
    }
    
	// 状态
	public $status = array(
			0 => '禁止',
			1 => '正常' 
	);
	
	// 捐赠开关
	public $donate_on = array(
	    0=>'关闭',
	    1=>'开启'
	);
	
	protected $_auto = array(
		array('reward_percent', 'getRewardPercent', self::MODEL_BOTH, 'callback'),
		array('reward_percent_dealer', 'getRewardPercentDealer', self::MODEL_BOTH, 'callback'),
        array('user_discount', 'getUserDiscount', self::MODEL_BOTH, 'callback'),
	    array('level_config', 'getLevelConfig', self::MODEL_BOTH, 'callback'),
	    array('config', 'getConfig', self::MODEL_BOTH, 'callback'),
	);
	
	// 自动验证
	protected $_validate = array(
			array(
				'name',
				'require',
				'商城名称不能为空',
				self::EXISTS_VALIDATE 
			) 
	);
	
	/**
	 * 
	 * @return string
	 */
	public function getConfig() {
	    $config = (array)I('config', array());
	    $submit = array();
	    /* 广告弹出 */
	    if ( isset($config['indexAdv']) ) {
	        $cur = $this->extendsConfig['indexAdv'];
	        $cur['value'] = htmlspecialchars_decode($config['indexAdv']);
	        $submit['indexAdv'] = $cur;
	    }
	    return serialize($submit);
	}
	
	/**
	 * 各角色是否开启等级
	 * @return string
	 */
	protected function getLevelConfig() {
	    $levelConfig = (array)I('level_config', array());
	    return serialize($levelConfig);
	}
	
	public function getUserDiscount() {
	    $userDiscount = $_POST['user_discount'];
	    if ( isset($_POST['user_discount']) ) {
	        return serialize($userDiscount);
	    }
	    return null;
	}
	
	/**
	 * 返拥金比例
	 */
	public function getRewardPercent() {
		$rewardPercent = I("reward_percent");
		$rewards = array();
		$tmp = explode(':', $rewardPercent);
		foreach ( $tmp as $v ) {
			$rewards[] = $v*1;
		}
		return join(':', $rewards);
	}
	
	/**
	 * 返拥金比例
	 */
	public function getRewardPercentDealer() {
		$rewardPercent = I("reward_percent_dealer");
		$rewards = array();
		$tmp = explode(':', $rewardPercent);
		foreach ( $tmp as $v ) {
			$rewards[] = $v*1;
		}
		return join(':', $rewards);
	}
	
	/**
	 * 获得商品配置信息
	 * @param unknown $cid
	 * @return Ambigous <mixed, \Think\mixed>
	 */
	public function getShopConfig( $cid = 0 ) {
	    $cid = $cid ? $cid : $this->adminId;
	    $config = M("DistributionShop")->cache("distribution_shop_cid_{$cid}", 180)->where("cid=%d", $cid)->find();
	    $configArray = array();
	    /* 扩展配置 */
	    $config['config'] and $configArray = unserialize($config['config']);
	    $config['config'] = array ();
	    foreach ($configArray as $k => $v) {
	        $config['config'][$v['name']] = $v['value'];
	    }
	    if ( empty($config['config']) ) { // 扩展配置
	        unset($config['config']);
	    }
	    /* 代理配置  */
	    if ( !empty($config['agent_shop_config']) ) {
	        $config['agent_shop_config'] = unserialize($config['agent_shop_config']);
	    } else {
	        unset($config['agent_shop_config']);
	    }
	    /* 等级开启配置  */
	    if ( !empty($config['level_config']) ) {
	        $config['level_config'] = unserialize($config['level_config']);
	    } else {
	        unset($config['level_config']);
	    }
	    /* 等级开启配置  */
	    if ( !empty($config['user_discount']) ) {
	        $config['user_discount'] = unserialize($config['user_discount']);
	    } else {
	        unset($config['user_discount']);
	    }
	    /* 城币配置  */
	    if ( !empty($config['currency_config']) ) {
	        $config['currency_config'] = unserialize($config['currency_config']);
	    } else {
	        unset($config['currency_config']);
	    }
	    return $config;
	}
	
	// 更新前加上权限条件
	protected function _after_update($data, $options) {
	    S("distribution_shop_cid_{$this->adminId}", null);
	}
}